import { isArray } from "@/utils/is";
import { dayjs } from "element-plus";
/**
 * @description 格式化表格单元格默认值(el-table-column)
 * @param {Number} row 行
 * @param {Number} col 列
 * @param {String} callValue 当前单元格值
 * @return string
 * */
export function defaultFormat(row: number, col: number, callValue: any) {
	// 如果当前值为数组,使用 / 拼接（根据需求自定义）
	if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
	return callValue ?? "--";
}

/**
 * @description 处理无数据情况
 * @param {String} callValue 需要处理的值
 * @return string
 * */
export function formatValue(callValue: any) {
	// 如果当前值为数组,使用 / 拼接（根据需求自定义）
	if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
	return callValue ?? "--";
}

export interface DataFormat {
	date: "date";
}
export type DateFormatType = "date" | "datetime" | "time";
/**
 * 数据格式化
 * @param target 格式化处理器
 * @param value 需要格式的内容
 * @returns tesult
 * @example console.log(dataFormat("datetime", "2023-11-15"))
 */
export function dataFormat(target: DateFormatType, value: any) {
	let proxy = proxyFormat;
	let call = proxy[target];
	if (!call) {
		return "-";
	}
	return call(value);
}

const proxyFormat: { [key: string]: any } = {
	date: (val: string) => {
		let dts = dayjs(val);
		if (!dts.isValid()) {
			return "-";
		}
		return dts.format("YYYY-MM-DD");
	},
	time: (val: string) => {
		let dts = dayjs(val);
		if (!dts.isValid()) {
			return "-";
		}
		return dts.format("HH:mm:ss");
	},
	datetime: (val: string) => {
		let dts = dayjs(val);
		if (!dts.isValid()) {
			return "-";
		}
		return dts.format("YYYY-MM-DD HH:mm:ss");
	}
};

export function duration(time: any) {
	if (!time.isValid()) {
		return "0秒";
	}
	let start = time.valueOf();
	let end = dayjs().valueOf();
	let diff = end - start;
	return convertTime(diff);
}
export function convertTime(time: number) {
	time = time / 1000;
	let res: string = "";
	let unit;
	if (time / 60 < 1) {
		res = time.toFixed(1);
		unit = "秒";
	} else if (time / 60 / 60 < 1) {
		res = (time / 60).toFixed(1);
		unit = "分钟";
	} else if (time / 60 / 60 / 24 < 1) {
		res = (time / 60 / 60).toFixed(1);
		unit = "小时";
	} else if (time / 60 / 60 / 24 / 30 < 1) {
		res = (time / 60 / 60 / 24).toFixed(1);
		unit = "天";
	} else if (time / 60 / 60 / 24 / 30 / 12 < 1) {
		res = (time / 60 / 60 / 24 / 30).toFixed(1);
		unit = "月";
	} else {
		res = "0.0";
		unit = "秒";
	}
	// if (time / 60 < 1) {
	//   time = time.toFixed(1)
	//   unit = '秒'
	// } else {
	//   time = (time / 60).toFixed(2)
	//   unit = '分钟'
	// }
	let data = res.split(".");
	let suffix = Number(data[1]);
	res = data[0] + (suffix != 0 ? "." + suffix : "") + unit;
	return res;
}

/**
 * 流量格式
 * @param flow
 * @returns {{flow: any, unit: *}}
 */
export function formatSize(val: Number | any): { size: number; unit: string; scale: number; full: string } {
	let size = isNaN(val) || !val ? 0 : Number(val);
	let flowFixed: string;
	let scale = 1;
	let unit;
	if (size / 1024 < 1) {
		flowFixed = size.toFixed(1);
		unit = "B";
		scale = 1;
	} else if (size / 1024 / 1024 < 1) {
		flowFixed = (size / 1024).toFixed(1);
		unit = "KB";
		scale = 1024;
	} else if (size / 1024 / 1024 / 1024 < 1) {
		flowFixed = (size / 1024 / 1024).toFixed(1);
		scale = 1024 * 1024;
		unit = "MB";
	} else if (size / 1024 / 1024 / 1024 / 1024 < 1) {
		flowFixed = (size / 1024 / 1024 / 1024).toFixed(1);
		scale = 1024 * 1024 * 1024;
		unit = "GB";
	} else {
		flowFixed = (size / 1024 / 1024 / 1024).toFixed(1);
		scale = 1024 * 1024 * 1024;
		unit = "GB";
	}
	size = size % 1.0 == 0 ? parseInt(flowFixed) : size;
	return { size: size, unit: unit, scale: scale, full: size + unit };
}
export function formatSizeAndUnit(val: any) {
	let res = formatSize(val);
	return res.size + res.unit;
}
export function formatSeconds(value: number) {
	let secondTime = value; // 秒
	let minuteTime = 0; // 分
	let hourTime = 0; // 小时
	if (secondTime > 60) {
		//如果秒数大于60，将秒数转换成整数
		//获取分钟，除以60取整数，得到整数分钟
		minuteTime = parseInt(secondTime / 60 + "");
		//获取秒数，秒数取佘，得到整数秒数
		secondTime = parseInt((secondTime % 60) + "");
		//如果分钟大于60，将分钟转换成小时
		if (minuteTime > 60) {
			//获取小时，获取分钟除以60，得到整数小时
			hourTime = parseInt(minuteTime / 60 + "");
			//获取小时后取佘的分，获取分钟除以60取佘的分
			minuteTime = parseInt((minuteTime % 60) + "");
		}
		minuteTime += hourTime * 60;
	}
	let secondTimeRes = secondTime < 10 ? "0" + secondTime : secondTime;
	let minuteTimeRes = minuteTime < 10 ? "0" + minuteTime : minuteTime;
	return minuteTimeRes + ":" + secondTimeRes;
}

export function formatTimeDuration(time: number, simple: boolean) {
	let d = parseInt(time / 1000 / 60 / 60 / 24 + "", 10); //计算剩余的天数
	let h = parseInt(((time / 1000 / 60 / 60) % 24) + "", 10); //计算剩余的小时
	let m = parseInt(((time / 1000 / 60) % 60) + "", 10); //计算剩余的分钟
	let s = parseInt(((time / 1000) % 60) + "", 10); //计算剩余的秒数
	let val = "";
	if (simple) {
		if (d && h) {
			val = d + "天+";
		} else {
			if (h && m) {
				val = h + "时" + m + "分";
			} else if (m && s) {
				val += m + "分" + s + "秒";
			} else if (s) {
				val += s + "秒";
			}
		}
	} else {
		if (d) {
			val += d + "天";
		}
		if (h) {
			val += h + "时";
		}
		if (m) {
			val += m + "分";
		}
		if (s) {
			val += s + "秒";
		}
		if (!val) {
			val = "0秒";
		}
	}
	return val;
}

export const formatCron = (cron: string) => {
	let arr = cron.trim().split(" ");
	if (arr.length == 7 && arr[7] == "*") {
		arr.splice(6, 1);
	}
	let str = "";
	if (cron.includes("-") || cron.includes("#") || cron.includes("/")) {
		str = cron;
	} else {
		let second = arr[0] == "*" ? "每秒" : arr[0] + "秒";
		let minute = arr[1] == "*" ? "每分钟" : arr[1] + "分";
		let hour = arr[2] == "*" ? "每小时" : arr[2] + "时";
		let day = arr[3] == "*" ? "每天" : arr[3] + "日";
		let month = arr[4] == "*" ? "" : arr[4] + "月";
		let weeks = arr[5] == "?" ? "" : "星期" + arr[6];
		if (weeks) {
			str = `${weeks}，${hour}${minute}${second}`;
		} else {
			str = `${month}${day}，${hour}${minute}${second}`;
		}
		if (arr.length == 7) {
			str = arr[6] + "年，" + str;
		}
	}
	return str;
};
